<?php

namespace App\Console\Commands;

use App\Jobs\DigArticleChinaz;
use Carbon\Carbon;
use QL\QueryList;
use Illuminate\Support\Facades\DB;
use Illuminate\Console\Command;

class DigColumn extends Command
{
	//php artisan dig:ceshi 151 http://www.woshipm.com
    protected $client;
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'dig:column';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'dig:column';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
    	$this->info('开始运行！！！请等待');
        $data   = $this->check('https://top.chinaz.com');
        if($data){
        	
        	$out = preg_replace("'<script[^>]*?>.*?</script>'si", '', $data);
	    	$html = QueryList::html($out)->find('.TopNav-MenuItemThree .MenuItemCent');
	        $ql = QueryList::html($html->html())->rules([
	            //采集规则
	            'link'  => ['a','href'],
	            'title' => ['a','text'],
	        ])->range('.MenuItem-SubList li')->query()->getData(function ($item){
	        	$this->info($item['title'].'开始采集！！！');
				$rt = QueryList::get('https:'.$item['link']);
				$arrayRows = $rt->find('.TopListCent-Head .HeadFilter')->map(function($row){
				    return $row->find('a')->texts()->all();
				});
				$item['array']	= $arrayRows->all()[0];
				$id = DB::table('columns')->insertGetId([
                    'parent_id'		=> 0,
                    'type'			=> 0,
                    'order'			=> 0,
                    'title'			=> $item['title'],
                    'created_at'    => date('Y-m-d H:i:s', time()),
                    'updated_at'    => date('Y-m-d H:i:s', time()),
                ]);
                if($id){
                	foreach($item['array'] as $vv)
                	{
                		DB::table('columns')->insertGetId([
		                    'parent_id'		=> $id,
		                    'type'			=> 0,
		                    'order'			=> 0,
		                    'title'			=> $vv,
		                    'created_at'    => date('Y-m-d H:i:s', time()),
		                    'updated_at'    => date('Y-m-d H:i:s', time()),
		                ]);
                	}
                }
                $this->info($item['title'].'结束采集！！！');
	            return $item;
	        });
	        $data   = $ql->all();
	        $this->info('结束！！！');
        }else{
            $this->error('dig article error!');
        }
    }	
    
    //抓取网址
    function check($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);//抓取网址
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Baiduspider/3.0; +http://www.baidu.com/search/spider.html)");//伪造百度蜘蛛头部
        $ip = '220.181.7.121';
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:' . $ip, 'CLIENT-IP:' . $ip));//伪造百度蜘蛛IP
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//
//        curl_setopt($ch, CURLOPT_HEADER, true); //抓取服务器信息
        curl_setopt($ch, CURLOPT_NOBODY, false); //抓取网页信息
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //支持301跳转
        curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 30); //网页等待时间
        $data = curl_exec($ch);
        $code = curl_getinfo($ch);
        curl_close($ch);
        if($code['http_code'] == 200){
            return $data;
//            $parse_url  = parse_url($code['url']);
//            return      $parse_url['scheme'].'://'.$parse_url['host'];
        }else{
            return false;
        }
    }
}
